package com.cyq.blog.config;

import com.cyq.blog.intercepter.AdminIntercepter;
import com.cyq.blog.intercepter.RedisUrlIntercepter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * MyConfig class
 *
 * @author chenyeqing
 * @date 2021/5/14
 */
@Configuration
public class MyConfig implements WebMvcConfigurer {

    @Autowired
    AdminIntercepter adminIntercepter;

    @Autowired
    RedisUrlIntercepter redisUrlIntercepter;

    /**
     * 拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(adminIntercepter)
                .addPathPatterns("/admin/**")
                //.excludePathPatterns("css/**","img/**", "js/**")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/admin/login.html");
        // 拦截具体博客统计访问量
        registry.addInterceptor(redisUrlIntercepter)
                .addPathPatterns("/blog/**")
                .excludePathPatterns("/**/*.png")
                .excludePathPatterns("/blog/search");
    }

    /**
     * 国际化
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }
}
